import 'package:FineArt/components/horizon_scroll.dart';
import 'package:FineArt/components/rounded_text.dart';
import 'package:FineArt/constants.dart';
import 'package:FineArt/core/api/module_api.dart';
import 'package:FineArt/core/api/resource_api.dart';
import 'package:FineArt/core/utils/toast.dart';
import 'package:FineArt/core/widget/grid/grid_item.dart';
import 'package:FineArt/core/widget/grid/grid_item_photo.dart';
import 'package:FineArt/core/widget/grid/grid_item_teach.dart';
import 'package:FineArt/core/widget/list/article_item.dart';
import 'package:FineArt/page/tab/recommend/component/recommendItem.dart';
import 'package:FineArt/utils/log_util.dart';
import 'package:FineArt/utils/size_config.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_swiper/flutter_swiper.dart';

import 'RoundCheckBox.dart';

class ChooseFolderPage extends StatefulWidget {
  final Resource resource;

  ChooseFolderPage({this.resource});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _ChooseFolderPageState();
  }
}

class _ChooseFolderPageState extends State<ChooseFolderPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    buildAction();
  }



  int _count = 3;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var width = MediaQuery.of(context).size.width * 0.76;

    return Scaffold(
      backgroundColor: Color.fromRGBO(0xff, 0xff, 0xff, 0.26),
      body: Padding(
          padding: EdgeInsets.symmetric(
              vertical: 0, horizontal: getPortraitSWidth(16)),
          child: Column(
            children: [
              SizedBox(
                height: getPortraitSHeight(30),
              ),
              Padding(
                padding: EdgeInsets.symmetric(vertical: 2, horizontal: 2),
                child: Row(
                  children: [
                    InkWell(
                      child: Image.asset(
                        "assets/images/icon_folder_close.png",
                        width: getPortraitSWidth(30),
                        height: getPortraitSHeight(30),
                      ),
                      onTap: () {
                        Navigator.of(context).pop();
                      },
                    ),
                    Expanded(
                      child: Text(""),
                    ),
                    InkWell(
                      child: Text(
                        "确定",
                        style:
                            TextStyle(color: Colors.white, fontSize: setSp(15)),
                      ),
                      onTap: () {
                        LogUtil.d(" 确定 onclick");
                        // this.startFavorite();
                        Navigator.of(context).pop();
                      },
                    )
                  ],
                ),
              ),
              Expanded(
                  child: GridView.builder(
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 3, //每行三列
                        childAspectRatio: 1.0, //显示区域宽高相等
                        crossAxisSpacing: 3.0,
                        // 定义行边距
                        mainAxisSpacing: 3.0,
                      ),
                      itemCount: actions.length,
                      itemBuilder: (context, index) {
                        return getFolderItem(index, () {
                          LogUtil.d("item click $index");
                          List<FolderBean> tmpAct = actions;
                          if (index == actions.length - 1) {
                            for (int i = 0; i < tmpAct.length; i++) {
                              tmpAct[i].isCheck = false;
                            }
                            ToastUtils.toast("添加");
                          } else {
                            for (int i = 0; i < tmpAct.length; i++) {
                              tmpAct[i].isCheck = index == i ? true : false;
                            }
                          }
                          setState(() {
                            actions = tmpAct;
                          });
                        });
                      })),
            ],
          )),
    );
  }

  int serverFolderCount = 1;

  Widget getFolderItem(int index, Function onTab) {
    FolderBean fd = actions[index];
    if (fd.name.isEmpty) {
      fd.name = "默认";
    }

    bool isAdd = index == serverFolderCount;
    return InkWell(
        onTap: onTab,
        child: Container(
          color: Color.fromRGBO(0x00, 0x00, 0x00, 0.74),
          child: Stack(alignment: Alignment.center, children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Image.asset(
                  fd.imageUrl,
                  width:
                      isAdd ? getPortraitSWidth(45) : getPortraitSWidth(59.5),
                ),
                isAdd
                    ? SizedBox(
                        height: 0,
                      )
                    : Text(
                        fd.name,
                        style:
                            TextStyle(color: Colors.white, fontSize: setSp(12)),
                      )
              ],
            ),
            Positioned(
                bottom: 0,
                right: 0,
                child: actions[index].isCheck
                    ? RoundCheckBox(
                        value: true, isRadio: true, onChanged: (bool val) {})
                    : Text("")),
          ]),
        ));
  }

  List<FolderBean> actions = [];

  void buildAction() {
    for (int i = 0; i < serverFolderCount + 1; i++) {
      String url = "assets/images/icon_folder.png";
      if (i == serverFolderCount) {
        url = "assets/images/icon_add_folder.png";
      }
      actions.add(FolderBean(url, "", i.toString()));
    }
  }
}

class FolderBean {
  String imageUrl;
  String name;
  String id;
  bool isCheck = false;

  FolderBean(this.imageUrl, this.name, this.id);
}
